package org.maverickjs.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebFilter(urlPatterns={"/pages/chat/chat.jsp"})
public class ChatFilter implements Filter{
	
	public void init(FilterConfig evt) throws ServletException {
		
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		
		try {			
			
			HttpServletRequest httpRequest = (HttpServletRequest) request;
			HttpServletResponse httpResponse = (HttpServletResponse) response;
			
			if (httpRequest.getSession().getAttribute("user") != null)			
				filterChain.doFilter(request, response);
			else
				httpResponse.sendRedirect("login.jsp");
			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void destroy() {

	}

}
